********************************************************************************
********"Macroprudential Regulation, Quantitative Easing, and Bank Lending"*****
********************************************************************************
log using $outputdirectory/TAB1, replace
log off

********************************************************************************
********************************************************************************
**********************************2019******************************************
********************************************************************************
********************************************************************************
**#Bookmark (input)
clear *
import sas ENTE_SEGN CENSITO_CRITTOG TIME DISBUR COM COMUNE_DIP using $inputdirectory/bloans2019, case(lower)
run RO_creditregister tm(2019m2) tm(2020m2) 0
keep bankid borrowerid time disbur

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_gru2019, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
drop bankid
rename gru bankid
collapse (sum) disbur, by(bankid borrowerid time)

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2019m8, keepus(e10 e14 e13 e12 dtop66e14 dmid6633e14 exlnassets excashas)
keep if _merge==3
drop _merge

**#Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2019, keepus(lncrefin)
keep if _merge!=2
drop _merge

egen id=group(bankid borrowerid)
xtset id time
gen lncdisbur=100*(ln(disbur)-ln(L.disbur))
keep if time>=tm(2019m3)&time<=tm(2020m2)

keep if time>=tm(2019m3)&time<=tm(2020m2)

egen byte nrel=count(bankid), by(time borrowerid)
gen byte ones=1
bys borrowerid: gen byte unbor=sum(ones)
bys borrowerid time: gen byte unbortime=sum(ones)
drop ones
replace nrel=6 if nrel>=6
gen dnrel=(nrel>1)

log on
**#Bookmark (output - panel a and panel b)
tab nrel if nrel>1&unbortime==1&time==tm(2019m8)
tabstat disbur if time==tm(2019m8), by(dnrel) s(n mean p25 median p75 sd) c(s) format(%12.0g)
tabstat lncdisbur, by(dnrel) s(n mean p25 median p75 sd) c(s) format(%12.0g)
log off 


*==================================================================================
**#Bookmark (input)
clear *
import sas using $inputdirectory/taeg2019ana, case(lower)

duplicates drop data_oss cod_oa censito_crittog cntrct_id instrmnt_id, force
rename ente_segn bankid
rename censito_crittog borrowerid
format borrowerid %20.0g

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_gru2019, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
drop bankid
rename gru bankid

replace data_oss=data_oss*1000
gen xxx=dofc(data_oss)
gen time=mofd(xxx)
format time %tm
drop data_oss
keep if time>=tm(2019m3)&time<=tm(2020m2)

collapse (sum) acc taeg_acc, by(bankid borrowerid time)

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2019m8, keepus(dtop66e14 dmid6633e14 exlnassets excashas)
keep if _merge==3
drop _merge

**#Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2019, keepus(lncrefin)
keep if _merge!=2
drop _merge

gen taeg=100*taeg_acc/acc

egen byte nrel=count(bankid), by(time borrowerid)
gen dnrel=(nrel>1)

**#Bookmark (output - panel c)
log on
tabstat taeg, by(dnrel) s(n mean p25 median p75 sd cv) c(s) format(%12.0g) save
log off

********************************************************************************
********************************************************************************
**********************************2015******************************************
********************************************************************************
********************************************************************************
**#Bookmark (input)
clear *
import sas ENTE_SEGN CENSITO_CRITTOG TIME DISBUR COM COMUNE_DIP using $inputdirectory/bloans, case(lower)
run RO_creditregister tm(2014m6) tm(2015m6) 0
keep bankid borrowerid time disbur

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_gru2015, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
drop bankid
rename gru bankid
collapse (sum) disbur, by(bankid borrowerid time)

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2014m12, keepus(e10 e13 dtop85e13 exlnassets excashas)
keep if _merge==3
drop _merge

**#Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2015, keepus(lncrefin)
keep if _merge!=2
drop _merge

egen id=group(bankid borrowerid)
xtset id time
gen lncdisbur=100*(ln(disbur)-ln(L.disbur))
keep if time>=tm(2014m7)&time<=tm(2015m6)

egen byte nrel=count(bankid), by(time borrowerid)
gen byte ones=1
bys borrowerid: gen byte unbor=sum(ones)
bys borrowerid time: gen byte unbortime=sum(ones)
drop ones
replace nrel=6 if nrel>=6
gen dnrel=(nrel>1)

log on
**#Bookmark (output - panel a and panel b)
tab nrel if nrel>1&unbortime==1&time==tm(2014m12)
tabstat disbur if time==tm(2014m12), by(dnrel) s(n mean p25 median p75 sd) c(s) format(%12.0g)
tabstat lncdisbur, by(dnrel) s(n mean p25 median p75 sd) c(s) format(%12.0g)
log off

*==================================================================================
**#Bookmark (input)
clear *
import sas ENTE_SEGN CENSITO_CRITTOG DATA_OSS VOCESOTVOC VALORE DURATA DURORI_TASSO TIP_ATT using $inputdirectory/taeg, case(lower)

rename ente_segn bankid
rename censito_crittog borrowerid
format borrowerid %20.0g

reshape wide valore, i(data_oss bankid borrowerid durata durori_tasso tip_att) j(vocesotvoc)

rename valore5504213 taeg
rename valore5504233 amount
replace taeg=taeg/10000

replace data_oss=data_oss*1000
gen xxx=dofc(data_oss)
gen time=qofd(xxx)
format time %tq
drop data_oss
drop xxx

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_gru2015, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
drop bankid
rename gru bankid

gen xxx=taeg*amount
collapse (sum) xxx amount, by(bankid borrowerid time)

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2014m12, keepus(dtop85e13 exlnassets excashas)
keep if _merge==3
drop _merge

**#Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinq2015
keep if _merge!=2
drop _merge
recode lncrefin (.=0)

keep if time>=tq(2014q3)
keep if time<=tq(2015q2)

egen id=group(bankid borrowerid)
xtset id time

gen taeg=xxx/amount
drop xxx

egen byte nrel=count(bankid), by(time borrowerid)
gen dnrel=(nrel>1)

log on 
**#Bookmark (output - panel c)
tabstat taeg, by(dnrel) s(n mean p25 median p75 sd cv) c(s) format(%12.0g)
log off
log close



